'''
Case Type： 故障&可靠性测试
Case Name： 数据库主机控制文件权限丢失，备机异常重建。
Create At: 2020/07/20
@zou_jialiang0505328114
Descption:
步骤 1.启动主备数据库，判断数据库状态；
步骤 2.修改主机控制文件权限
步骤 3.kill掉备机进程
步骤 4.备机重建；
'''
import os
import unittest
import _thread
import time
from yat.test import macro
from yat.test import Node
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import *


class Deletaduit(unittest.TestCase):
    primary_sh = CommonSH('PrimaryDbUser')
    standby1_sh = CommonSH('Standby1DbUser')
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('------------Opengauss_Reliability_Dbsys_Case142.py start------------')
        self.com = Common()
        self.Constant = Constant()
        self.rootNode = Node()
        self.primary_user_node = Node(node='PrimaryDbUser')
        self.primary_root_node = Node(node='PrimaryRoot')
        self.standby1_root_node = Node(node='Standby1Root')
        self.standby1_user_node = Node(node='Standby1DbUser')

    def test_startdb(self):
        try:
            # 修改主机控制文件权限
            shell_cmd = f"chmod 000 {macro.PG_CONTROL_PATH}"
            self.log.info(shell_cmd)
            msg = self.primary_root_node.sh(shell_cmd).result()
            self.log.info(msg)
            self.assertEqual(msg, '')

            # kill掉备机进程
            msg1 = self.com.kill_pid(self.standby1_root_node, 9)
            self.assertEqual(msg1, '')

            # 查看集群状态
            status = self.primary_sh.getDbClusterStatus('detail')
            self.log.info(status)
            self.assertIn('stopped', status)

            # 备机重建；
            self.log.info('无权限时备机重建')
            build_msg = self.standby1_sh.executeGsctl('build', self.Constant.REBUILD_SUCCESS_MSG, '-b full',
                                                      get_detail=True)
            self.log.info(build_msg)
            self.assertNotIn(self.Constant.BUILD_SUCCESS_MSG, msg)

            time.sleep(5)
        finally:
            # 还原权限
            shell_cmd = f'chmod 600 {macro.PG_CONTROL_PATH}'
            self.log.info(shell_cmd)
            msg = self.primary_root_node.sh(shell_cmd).result()
            self.log.info(msg)
            self.assertEqual(msg, '')

            # 启动主节点
            msg = self.primary_sh.executeGsctl('restart', 'self.Constant.RESTART_SUCCESS_MSG', '-M primary', get_detail=True)
            self.assertTrue(msg)

            self.log.info('恢复权限后备机重建')
            build_msg = self.standby1_sh.executeGsctl('build', self.Constant.REBUILD_SUCCESS_MSG, '-b full')
            self.assertTrue(build_msg)

            time.sleep(5)
            # 查看集群状态
            status = self.primary_sh.getDbClusterStatus('detail')
            self.log.info(status)

    def tearDown(self):
        self.log.info('--------------Opengauss_Reliability_Dbsys_Case142.py finish------------')
